import json

def parse_event_data(filename):
    # Define the statistical structure
    event_stats = {}
    total_stats = {
        "Reads Queued": 0,
        "Writes Queued": 0,
        "Read Dispatches": 0,
        "Write Dispatches": 0,
        "Reads Completed": 0,
        "Writes Completed": 0,
        "Read Merges": 0,
        "Write Merges": 0,
        "Reads Queued KiB": 0,
        "Writes Queued KiB": 0,
        "Read Dispatches KiB": 0,
        "Write Dispatches KiB": 0,
        "Reads Completed KiB": 0,
        "Writes Completed KiB": 0,
        "Read Merges KiB": 0,
        "Write Merges KiB": 0,
    }

    # Read the JSON file
    with open(filename, "r") as f:
        for line in f:
            try:
                event = json.loads(line)
                cpu = f"CPU{event['cpu']}"
                rwflag = "Read" if event["rwflg"] == "R" else "Write"
                if cpu not in event_stats:
                    event_stats[cpu] = {
                        "Reads Queued": 0,
                        "Writes Queued": 0,
                        "Read Dispatches": 0,
                        "Write Dispatches": 0,
                        "Reads Completed": 0,
                        "Writes Completed": 0,
                        "Read Merges": 0,
                        "Write Merges": 0,
                        "Reads Queued KiB": 0,
                        "Writes Queued KiB": 0,
                        "Read Dispatches KiB": 0,
                        "Write Dispatches KiB": 0,
                        "Reads Completed KiB": 0,
                        "Writes Completed KiB": 0,
                        "Read Merges KiB": 0,
                        "Write Merges KiB": 0,
                    }

                event_type = event["event"]
                data_len_kb = event["len"] / 1024

                if event_type == "Q":
                    event_stats[cpu][f"{rwflag}s Queued"] += 1
                    event_stats[cpu][f"{rwflag}s Queued KiB"] += data_len_kb
                    total_stats[f"{rwflag}s Queued"] += 1
                    total_stats[f"{rwflag}s Queued KiB"] += data_len_kb
                elif event_type == "D":
                    event_stats[cpu][f"{rwflag} Dispatches"] += 1
                    event_stats[cpu][f"{rwflag} Dispatches KiB"] += data_len_kb
                    total_stats[f"{rwflag} Dispatches"] += 1
                    total_stats[f"{rwflag} Dispatches KiB"] += data_len_kb
                elif event_type == "C":
                    event_stats[cpu][f"{rwflag}s Completed"] += 1
                    event_stats[cpu][f"{rwflag}s Completed KiB"] += data_len_kb
                    total_stats[f"{rwflag}s Completed"] += 1
                    total_stats[f"{rwflag}s Completed KiB"] += data_len_kb
                elif event_type == "M":
                    event_stats[cpu][f"{rwflag} Merges"] += 1
                    event_stats[cpu][f"{rwflag} Merges KiB"] += data_len_kb
                    total_stats[f"{rwflag} Merges"] += 1
                    total_stats[f"{rwflag} Merges KiB"] += data_len_kb

            except json.JSONDecodeError as e:
                print(f"Error decoding JSON: {e}")
            except KeyError as e:
                print(f"Missing key in event data: {e}")

    for cpu in sorted(event_stats.keys()):
        stats = event_stats[cpu]
        print(f"{cpu}:")
        print(f" Reads Queued:           {stats['Reads Queued']},        {stats['Reads Queued KiB']:.0f}KiB\t Writes Queued:           {stats['Writes Queued']},        {stats['Writes Queued KiB']:.0f}KiB")
        print(f" Read Dispatches:        {stats['Read Dispatches']},        {stats['Read Dispatches KiB']:.0f}KiB\t Write Dispatches:        {stats['Write Dispatches']},        {stats['Write Dispatches KiB']:.0f}KiB")
        print(f" Reads Completed:        {stats['Reads Completed']},        {stats['Reads Completed KiB']:.0f}KiB\t Writes Completed:        {stats['Writes Completed']},        {stats['Writes Completed KiB']:.0f}KiB")
        print(f" Read Merges:            {stats['Read Merges']},        {stats['Read Merges KiB']:.0f}KiB\t Write Merges:            {stats['Write Merges']},        {stats['Write Merges KiB']:.0f}KiB")
        print()

    print("Total:")
    print(f" Reads Queued:           {total_stats['Reads Queued']},        {total_stats['Reads Queued KiB']:.0f}KiB\t Writes Queued:           {total_stats['Writes Queued']},        {total_stats['Writes Queued KiB']:.0f}KiB")
    print(f" Read Dispatches:        {total_stats['Read Dispatches']},        {total_stats['Read Dispatches KiB']:.0f}KiB\t Write Dispatches:        {total_stats['Write Dispatches']},        {total_stats['Write Dispatches KiB']:.0f}KiB")
    print(f" Reads Completed:        {total_stats['Reads Completed']},        {total_stats['Reads Completed KiB']:.0f}KiB\t Writes Completed:        {total_stats['Writes Completed']},        {total_stats['Writes Completed KiB']:.0f}KiB")
    print(f" Read Merges:            {total_stats['Read Merges']},        {total_stats['Read Merges KiB']:.0f}KiB\t Write Merges:            {total_stats['Write Merges']},        {total_stats['Write Merges KiB']:.0f}KiB")